using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Dm.Storage.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration;

namespace Microsoft.EntityFrameworkCore.Dm.ValueGeneration.Internal;

public class DmValueGeneratorSelector : RelationalValueGeneratorSelector
{
	private readonly IDmSequenceValueGeneratorFactory _sequenceFactory;

	private readonly IDmRelationalConnection _connection;

	private readonly IRawSqlCommandBuilder _rawSqlCommandBuilder;

	private readonly IRelationalCommandDiagnosticsLogger _commandLogger;

	public virtual IDmValueGeneratorCache Cache => (IDmValueGeneratorCache)((ValueGeneratorSelector)this).Cache;

	public DmValueGeneratorSelector([NotNull] ValueGeneratorSelectorDependencies dependencies, [NotNull] IDmSequenceValueGeneratorFactory sequenceFactory, [NotNull] IDmRelationalConnection connection, [NotNull] IRawSqlCommandBuilder rawSqlCommandBuilder, [NotNull] IRelationalCommandDiagnosticsLogger commandLogger)
		: base(dependencies)
	{
		_sequenceFactory = sequenceFactory;
		_connection = connection;
		_rawSqlCommandBuilder = rawSqlCommandBuilder;
		_commandLogger = commandLogger;
	}

	public override ValueGenerator Select(IProperty property, IEntityType entityType)
	{
		Check.NotNull<IProperty>(property, "property");
		Check.NotNull<IEntityType>(entityType, "entityType");
		if (((IReadOnlyProperty)property).GetValueGeneratorFactory() != null || ((IReadOnlyProperty)(object)property).GetValueGenerationStrategy() != DmValueGenerationStrategy.SequenceHiLo)
		{
			return ((ValueGeneratorSelector)this).Select(property, entityType);
		}
		return _sequenceFactory.Create(property, Cache.GetOrAddSequenceState(property), _connection, _rawSqlCommandBuilder, _commandLogger);
	}

	public override ValueGenerator Create(IProperty property, IEntityType entityType)
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Expected O, but got Unknown
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Expected O, but got Unknown
		Check.NotNull<IProperty>(property, "property");
		Check.NotNull<IEntityType>(entityType, "entityType");
		if (!(((IReadOnlyPropertyBase)property).ClrType.UnwrapNullableType() == typeof(Guid)))
		{
			return ((ValueGeneratorSelector)this).Create(property, entityType);
		}
		if ((int)((IReadOnlyProperty)property).ValueGenerated != 0 && RelationalPropertyExtensions.GetDefaultValueSql((IReadOnlyProperty)(object)property) == null)
		{
			return (ValueGenerator)new GuidValueGenerator();
		}
		return (ValueGenerator)new TemporaryGuidValueGenerator();
	}
}
